"""
虚拟机的Python包装器
"""
import ctypes
from ctypes import c_char_p, c_int, c_void_p, POINTER
import os
import sys

class PyVM:
    """
    虚拟机的Python封装类
    提供虚拟机的基本操作接口
    """
    def __init__(self, pool_size=256*1024):
        """初始化虚拟机"""
        # 加载动态链接库
        self.lib = self._load_library()
        
        # 设置函数参数和返回类型
        self._setup_function_types()
        
        # 初始化虚拟机
        self.lib.vm_init(pool_size)
    
    def _load_library(self):
        """加载动态链接库"""
        lib_path = self.get_lib_path()
        try:
            return ctypes.CDLL(lib_path)
        except Exception as e:
            raise Exception(f"无法加载虚拟机库: {e}")
    
    def _setup_function_types(self):
        """设置库函数的参数和返回类型"""
        # 基本函数
        self.lib.vm_init.argtypes = [c_int]
        self.lib.vm_init.restype = None
        
        self.lib.vm_free.argtypes = []
        self.lib.vm_free.restype = None
        
        self.lib.vm_get_status.argtypes = []
        self.lib.vm_get_status.restype = c_char_p
        
        # 内存段相关函数
        self.lib.vm_get_text.argtypes = []
        self.lib.vm_get_text.restype = POINTER(c_int)
        
        self.lib.vm_get_data.argtypes = []
        self.lib.vm_get_data.restype = c_char_p
        
        self.lib.vm_get_stack.argtypes = []
        self.lib.vm_get_stack.restype = POINTER(c_int)
        
        self.lib.vm_get_symbols.argtypes = []
        self.lib.vm_get_symbols.restype = POINTER(c_int)
        
        # 寄存器相关函数
        self.lib.vm_get_bp.argtypes = []
        self.lib.vm_get_bp.restype = c_int
        
        self.lib.vm_get_sp.argtypes = []
        self.lib.vm_get_sp.restype = c_int
        
        self.lib.vm_get_pc.argtypes = []
        self.lib.vm_get_pc.restype = c_int
        
        self.lib.vm_get_ax.argtypes = []
        self.lib.vm_get_ax.restype = c_int
        
        self.lib.vm_set_bp.argtypes = [c_int]
        self.lib.vm_set_bp.restype = None
        
        self.lib.vm_set_sp.argtypes = [c_int]
        self.lib.vm_set_sp.restype = None
        
        self.lib.vm_set_pc.argtypes = [c_int]
        self.lib.vm_set_pc.restype = None
        
        self.lib.vm_set_ax.argtypes = [c_int]
        self.lib.vm_set_ax.restype = None
    
    def get_lib_path(self):
        """获取库文件路径"""
        current_dir = os.path.dirname(os.path.abspath(__file__))
        lib_dir = os.path.join(os.path.dirname(current_dir), 'lib')
        if sys.platform.startswith('win'):
            return os.path.join(lib_dir, 'cvm2.dll')
        elif sys.platform.startswith('darwin'):
            return os.path.join(lib_dir, 'libcvm.dylib')
        else:
            return os.path.join(lib_dir, 'libcvm.so')
    
    def get_status(self):
        """获取虚拟机状态"""
        status = self.lib.vm_get_status()
        if status:
            return status.decode('utf-8')
        return ""
    
    # 提供寄存器访问接口
    @property
    def bp(self):
        return self.lib.vm_get_bp()
    
    @bp.setter
    def bp(self, value):
        self.lib.vm_set_bp(value)
    
    @property
    def sp(self):
        return self.lib.vm_get_sp()
    
    @sp.setter
    def sp(self, value):
        self.lib.vm_set_sp(value)
    
    @property
    def pc(self):
        return self.lib.vm_get_pc()
    
    @pc.setter
    def pc(self, value):
        self.lib.vm_set_pc(value)
    
    @property
    def ax(self):
        return self.lib.vm_get_ax()
    
    @ax.setter
    def ax(self, value):
        self.lib.vm_set_ax(value)
    
    def __del__(self):
        """释放虚拟机资源"""
        if hasattr(self, 'lib'):
            self.lib.vm_free() 